成長的架構
Rust 的集合,例如 Vec<T> 以及 String,並非基本類型;它們是位於 std 模組中的庫定義結構。此基礎決定了 Rust 如何透過模組系統組織資料,並透過 RAII(資源取得即初始化)來管理記憶體。雖然簡單類型存放在堆疊上,但集合則使用 堆記憶體儲存 以支援動態擴展,這表示其記憶體必須透過 Drop 特徵進行顯式管理。
模組解析與可見性
Rust 編譯器從套件根目錄(src/lib.rs 或 src/main.rs)開始建立模組樹。像 mod front_of_house; 這樣的宣告會促使編譯器搜尋 src/front_of_house.rs 或 src/front_of_house/mod.rs。使用 pub 修飾符與重新匯出(pub use)可讓封裝的堆記憶體資料透過符合慣例的路徑安全地介接。
一旦模組的作用域結束, Drop 實作就會自動回收堆記憶體:$$Memory_{reclaimed} = \sum Drop(Elements)$$。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
What happens if a project contains both
src/module.rs and src/module/mod.rs?The compiler prioritizes the directory style.
It triggers a compiler error due to ambiguity.
The files are merged automatically.
Rust ignores the .rs file.
✅ Correct!
Rust's module resolution rules forbid having both styles for the same module to avoid ambiguity.❌ Incorrect
The compiler cannot determine which file serves as the module root, resulting in an error.QUESTION 2
How does RAII ensure memory safety in collections?
By using a background garbage collector.
By checking references at runtime.
By tying heap memory to the scope of the owner variable.
By keeping all data on the stack.
✅ Correct!
RAII ensures that once the owner goes out of scope, resources are automatically freed.❌ Incorrect
Rust avoids garbage collection; RAII is a compile-time and scope-driven mechanism.QUESTION 3
Which file is considered the 'anchor' for module resolution in a library crate?
src/main.rs
$src/lib.rs$
mod.rs
Cargo.toml
✅ Correct!
In a library crate, src/lib.rs is the crate root where module trees begin.❌ Incorrect
src/main.rs is the root for binary crates, not library crates.QUESTION 4
What is the purpose of the
pub use statement?To allocate memory on the heap.
To re-export a name to provide a more idiomatic public API.
To private-label a module.
To increase compilation speed.
✅ Correct!
It allows you to flatten your module structure for external users while keeping internal organization clean.❌ Incorrect
Re-exporting doesn't change memory allocation; it changes visibility and path accessibility.QUESTION 5
Why can't Collections like
Vec be stored entirely on the stack?The stack cannot store integers.
They must have a fixed size known at compile time to stay on the stack.
The stack is too slow for collections.
Stack memory is never dropped.
✅ Correct!
Collections grow dynamically; the stack requires fixed sizes for stack frame calculation.❌ Incorrect
The stack is fast, but it is rigid; heap storage provides the necessary flexibility for growth.Architecting the Restaurant System
Module Resolution and Lifecycle Case Study
You are designing a restaurant management system. The waitlist is a heap-allocated collection managed inside a module named 'front_of_house'. You need to ensure the waitlist is accessible to the main restaurant logic but properly cleaned up after the dinner service (the scope) ends.
Q
If you move 'hosting' functions into their own file, what are the two valid path options for the hosting module file?
Solution:
The two valid paths are 'src/front_of_house/hosting.rs' or 'src/front_of_house/hosting/mod.rs'.
The two valid paths are 'src/front_of_house/hosting.rs' or 'src/front_of_house/hosting/mod.rs'.
Q
Explain how the 'Drop' trait prevents a memory leak if the waitlist vector is defined inside the 'hosting' module.
Solution:
When the 'front_of_house' module's owner goes out of scope, Rust calls the 'Drop' implementation for the waitlist vector. This automatically deallocates the heap memory used by the vector elements, ensuring no data 'outlives' its owner.
When the 'front_of_house' module's owner goes out of scope, Rust calls the 'Drop' implementation for the waitlist vector. This automatically deallocates the heap memory used by the vector elements, ensuring no data 'outlives' its owner.